/* -*-c++-*- */

/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2020 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */


#ifndef OSGEARTH_NETWORK_H
#define OSGEARTH_NETWORK_H 1

#include <vector>
#include <set>
#include <map>
#include <cassert>

namespace osgEarth
{

    // EdgeType and NodeType are keys for identifying the user's objects
    template<class EdgeType, class NodeType> class Network
    {
    public:
        // Edge and Node are our container types
        struct Edge;
        struct Node
        {
            Node() {}
            std::vector<EdgeType> edges;
        };
        struct Edge
        {
            Edge(const NodeType& node1, const NodeType& node2)
                {
                    nodes[0] = node1;
                    nodes[1] = node2;
                }
            NodeType nodes[2];
        };
        typedef std::map<EdgeType, Edge> EdgeMap;
        EdgeMap networkEdges;
        void addEdge(const EdgeType& edge, const NodeType& node1, const NodeType& node2)
        {
            networkEdges.insert(typename EdgeMap::value_type(edge, Edge(node1, node2)));
        }
        typedef std::map<NodeType, Node> NodeMap;
        NodeMap networkNodes;

        void buildNetwork()
        {
            for (typename EdgeMap::iterator edgeItr = networkEdges.begin();
                 edgeItr != networkEdges.end();
                ++edgeItr)
            {
                for (int i = 0; i < 2; ++i)
                {
                    Node& thisNode = networkNodes[edgeItr->second.nodes[i]];
                    thisNode.edges.push_back(edgeItr->first);
                }
            }
        }
    };
}
#endif // OSGEARTH_NETWORK_H

